package org.activitiDdemo;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.UserEntityImpl;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;

import java.awt.event.AdjustmentEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VacationApp {

	public static void main(String[] args) {
		 //创建流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        IdentityService identityService = processEngine.getIdentityService();
//        User user = identityService.newUser("123");
//        user.setFirstName("总监A");
//        identityService.saveUser(user);
//
//        Group group = identityService.newGroup("2");
//        group.setName("总监组");
//        identityService.saveGroup(group);
//
//        identityService.createMembership("123", "2");


//        /// 得到流程存储服务组件
        RepositoryService repositoryService = processEngine.getRepositoryService();
//        // 部署流程文件
//        repositoryService.createDeployment().addClasspathResource("bpmn/vacationRequest.bpmn").deploy();
        /// 得到运行时服务组件
        RuntimeService runtimeService = processEngine.getRuntimeService();
        // 获取流程任务组件
        TaskService taskService = processEngine.getTaskService();
        DeploymentBuilder deployment = repositoryService.createDeployment();
        deployment.addClasspathResource("bpmn/vacationRequest.bpmn").name("vacationRequest").key("key").category("category").tenantId("tenantId").deploy();
        // 启动流程
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("vacationRequest");
//        Task vacationApply  = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();

//            ProcessDefinition vacationRequest = repositoryService.createProcessDefinitionQuery().processDefinitionKey("vacationRequest").latestVersion().singleResult();
//            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionId(vacationRequest.getId()).singleResult();

//            Task auditTask = taskService.createTaskQuery().processInstanceId(processInstance.getId()).processDefinitionId(vacationRequest.getId()).singleResult();

//            auditTask.setAssignee("经理A");
//            taskService.complete(auditTask.getId());
//            System.out.println(auditTask);


//
//        vacationApply.setAssignee("小C");
//        Map<String, Object> param = new HashMap<>();
//        param.put("days",  2);
//
//        taskService.complete(vacationApply.getId(), param);


        System.exit(0);
	}

}
